<!DOCTYPE html>
<html>
    <head>
        <title>Pick-up Game</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <script type="text/javascript" src="index.js"></script>
    </head>

    <body>

        <?php include_once('menu.php') ?>

        <?php
        if (!empty($_SESSION['username'])) :

            // connect to database
            $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
            $conn = pg_connect($conn_str);

            // query database
            $query = 'SELECT gameid,status FROM rosters WHERE username=\'' . $_SESSION['username'] . '\'';
            $result = pg_query($conn, $query);
            ?>
            
        <div class="container">
            <h2>My Games</h2>
            <hr></hr>
                <table class="displayTable">
                    <tr>
                        <td>Skill</td>
                        <td>Sport</td>
                        <td>Roster</td>
                        <td>Start</td>
                        <td>End</td>
                        <td>Status</td>
                    </tr>
                    <?php
                    while ($player = pg_fetch_assoc($result)) {

                        $query = 'SELECT * FROM games WHERE gameid=\'' . $player['gameid'] . '\'';
                        $result_game = pg_query($conn, $query);

                        $game = pg_fetch_assoc($result_game);
                        ?>
                        <tr class="hand" onClick="location.href='gameDetails.php?gameid=<?= $player['gameid'] ?>';">
                            <td><?= $game['skill'] ?></td>
                            <td><?= $game['sport'] ?></td>
                            <td><?= $game['rostersize'] ?></td>
                            <td><?= $game['start'] ?></td>
                            <td><?= $game['endtime'] ?></td>
                            <td>
                                <?php
                                if ($player['status'] == 3) {
				    print 'organizer';
                                } else if ($player['status'] == 2) {
                                    print 'active';
                                } else if ($player['status'] == 1) {
                                    print 'invited';
                                } else if ($player['status'] == 4) {
                                    print 'played';
                                } else {
				    print 'requested';
				}
                                ?></td>
                        </tr>
                    <?php } ?>
                </table>
            <?php
        else :
            header('Location: /');
        endif;
        ?>
    </div>
    </body>